package com.avic.map.service.impl;

import com.avic.map.common.model.enums.MapTaggingEnums;
import com.avic.map.common.util.AssertUtil;
import com.avic.map.common.util.ex.BizErrorCode;
import com.avic.map.common.util.ex.BizException;
import com.avic.map.dal.model.MapTagging;
import com.avic.map.service.MapTaggingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author huangsheng on 2018/12/22
 */
@Service
@Slf4j
public class MapTaggingServiceImpl extends AbstractServiceImpl implements MapTaggingService {
    @Override
    public List<MapTagging> wrongUavRecords(MapTaggingEnums mapTaggingEnums) {
        return mapTaggingMapper.wrongUavRecords(mapTaggingEnums.getType());
    }

    @Override
    public List<MapTagging> uavRecords(MapTaggingEnums mapTaggingEnums) {
        return mapTaggingMapper.uavRecords(mapTaggingEnums.getType());
    }

    @Override
    public int create(MapTagging mapTagging) {
        try {
            mapTaggingMapper.insert(mapTagging);
            return mapTagging.getId();
        } catch (Exception e) {
            log.error("新增标注数据发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void update(MapTagging mapTagging) {
        try {
            if (mapTagging.getTaggingType() == MapTaggingEnums.UAV.getType()) {
                log.info("标注类型是无人机，检查绑定的无人机是否已经绑定了");
                AssertUtil.isTrue(mapTaggingMapper.checkUavMapTagging(MapTaggingEnums.UAV.getType(), mapTagging.getUavId()) > 0,
                        BizErrorCode.PARAM_ERROR, "无人机已经绑定，不能再次绑定");
            }
            mapTaggingMapper.update(mapTagging);
        } catch (Exception e) {
            log.error("更新标注数据发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void remove(int id) {
        try {
            mapTaggingMapper.delete(id);
        } catch (Exception e) {
            log.error("删除标注数据发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void clear() {
        try {
            mapTaggingMapper.clear();
        } catch (Exception e) {
            log.error("清除标注数据发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }
}
